var UserCon = iClass("User");
var role = require("../lib/role");
module.exports = {
	prefix: "/user",
	get: {
		"/": function() {
			var user = role.getUser(this);
			this.body = ResponObj("json", user);
		},
		"/loginout": function() {
			var user = role.getUser(this);
			role.removeUser(this, user);
			this.body = ResponObj("json", "用户下线成功");
		},
		"/find_by_name/:search_name": function() {
			var search_name = this.params.search_name;
			var user_list = UserCon.findUser(search_name);
			this.body = ResponObj("json", user_list);
		}
	},
	post: {
		"/register": function() {
			var user_info = this.request.body;
			if (!(user_info.password && user_info.confirm_password)) {
				throw "表单不完整"
			} else if (user_info.password !== user_info.confirm_password) {
				throw "两次密码输入不匹配"
			} else {
				var new_user = new UserCon(user_info);
				this.body = ResponObj("json", new_user);
			}
		},
		//用户登录
		"/login": function() {
			var query = this.request.query;
			var form_data = this.request.body;
			//登录或者抛出异常
			var user = UserCon.login(form_data);
			role.setUser(this, user);
			this.body = ResponObj("json", user);
		},
	},
	put: {
		"/info": function() {
			var user = role.getUser(this);
			var user_info = this.request.body;
			user.setInfo(user_info);
			this.body = ResponObj("json", user);
		}
	}
};